QuickOPC User's Guide and Reference
WriteAllBytes(IEasyUAFileTransfer,UAEndpointDescriptor,UANamedNodeDescriptor,Byte[]) Method
Example 



OpcLabs.EasyOpcUA Assembly > OpcLabs.EasyOpc.UA.IO.Extensions Namespace > IEasyUAFileTransferExtension2 Class > WriteAllBytes Method : WriteAllBytes(IEasyUAFileTransfer,UAEndpointDescriptor,UANamedNodeDescriptor,Byte[]) Method
The OPC UA file transfer object that will perform the operation.
Endpoint descriptor. Identifies the OPC-UA server.
The named node descriptor of the file to which the data should be written.
Data to be written to the file.
Writes binary data to an OPC UA file.
Syntax
'Declaration
 
<ExtensionAttribute()>
Public Overloads Shared Sub WriteAllBytes( _
   ByVal fileTransfer As IEasyUAFileTransfer, _
   ByVal endpointDescriptor As UAEndpointDescriptor, _
   ByVal fileNamedNodeDescriptor As UANamedNodeDescriptor, _
   ByVal data() As Byte _
) 
'Usage
 
Dim fileTransfer As IEasyUAFileTransfer
Dim endpointDescriptor As UAEndpointDescriptor
Dim fileNamedNodeDescriptor As UANamedNodeDescriptor
Dim data() As Byte
 
IEasyUAFileTransferExtension2.WriteAllBytes(fileTransfer, endpointDescriptor, fileNamedNodeDescriptor, data)

Parameters

fileTransfer
The OPC UA file transfer object that will perform the operation.
endpointDescriptor
Endpoint descriptor. Identifies the OPC-UA server.
fileNamedNodeDescriptor
The named node descriptor of the file to which the data should be written.
data
Data to be written to the file.
Exceptions
ExceptionDescription

A null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.

This is a usage error, i.e. it will never occur (the exception will not be thrown) in a correctly written program. Your code should not catch this exception.

An I/O error has occurred.

This is an operation error that depends on factors external to your program, and thus cannot be always avoided. Your code must handle it appropriately.

Remarks

If the OPC UA file does not already exist, the method creates it.

This method is similar to FileSystem.WriteAllBytes Method for normal (operating system) files.

CAUTION: This method does not throw the usual OpcLabs.EasyOpc.UA.OperationModel.UAException. Instead, an System.IO.IOException is thrown in case of an error during the operation.

This method overload overwrites the file contents (does not append to it).

For this method to succeed if it is creating a new file, the fileNamedNodeDescriptor must contain both the named node path (of the parent directory) in the OpcLabs.EasyOpc.UA.Navigation.UANamedNodeDescriptor.NamedNodePath property, and the node name (name of the file) in the OpcLabs.EasyOpc.UA.Navigation.UANamedNodeDescriptor.NodeName property. If this method is opening an existing stream, the file can also be specified using just the final node descriptor (node of the file object) in the OpcLabs.EasyOpc.UA.Navigation.UANamedNodeDescriptor.FinalNodeDescriptor property.

Note: This method method opens a file, writes to it, and then closes it. Code that uses this method is simpler than code that uses a System.IO.BinaryWriter object with a System.IO.Stream. However, if you are adding data to a file using a loop, a System.IO.BinaryWriter object can provide better performance because you only have to open and close the file once.

See OPC UA File Transfer internals for information about Stream buffering.

Recommendation: Whenever possible, do not use this method directly, and use a higher-level abstraction instead. You can obtain a writable file provider (using GetWritableFileProvider), and make use of its methods, and methods available on the writable directory contents (obtained using OpcLabs.BaseLib.Extensions.FileProviders.IWritableDirectory.GetWritableDirectoryContents) or writable file info (obtained using OpcLabs.BaseLib.Extensions.FileProviders.IWritableDirectory.GetWritableFileInfo).

Example

.NET

// Shows how to write the full contents of an OPC UA file at once, using the file transfer client.
// Note: Consider using a higher-level abstraction, OPC UA file provider, instead.

using System;
using System.IO;
using System.Text;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.FileTransfer;
using OpcLabs.EasyOpc.UA.IO.Extensions;

namespace UADocExamples.FileTransfer._EasyUAFileTransferClient
{
    class WriteAllBytes
    {
        public static void Main1()
        {
            // Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe)
            UAEndpointDescriptor endpointDescriptor = "opc.tcp://localhost:48030";

            // A node that represents an instance of OPC UA FileType object.
            UANodeDescriptor fileNodeDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile";
            
            // Instantiate the file transfer client object
            var fileTransferClient = new EasyUAFileTransferClient();

            // Write all contents into a specified file node.
            byte[] bytes = Encoding.UTF8.GetBytes("TEXT FROM FILE TRANSFER CLIENT EXAMPLE. Demonstrates writing the whole contents of a file at once.");
            try
            {
                Console.WriteLine("Writing the whole file...");
                fileTransferClient.WriteAllBytes(endpointDescriptor, fileNodeDescriptor, bytes);

                // Due to an issue in the server, the file might not be readable now, without server restart.
                //Console.WriteLine("Reading the data back...");
                //byte[] data = fileTransferClient.ReadAllFileBytes(endpointDescriptor, fileNodeDescriptor);
                //Console.WriteLine(Encoding.UTF8.GetString(data));
            }
            // Beware that WriteAllFileBytes throws IOException and not UAException.
            catch (IOException ioException)
            {
                Console.WriteLine("*** Failure: {0}", ioException.GetBaseException().Message);
                return;
            }

            Console.WriteLine();
            Console.WriteLine("Finished...");
        }
    }
}
# Shows how to write the full contents of an OPC UA file at once, using the file transfer client.
# Note: Consider using a higher-level abstraction, OPC UA file provider, instead.

# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc

# Import .NET namespaces.
from System.Text import *
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.Extensions import *
from OpcLabs.EasyOpc.UA.FileTransfer import *
from OpcLabs.EasyOpc.UA.IO.Extensions import *
from OpcLabs.EasyOpc.UA.Navigation import *


# Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe).
endpointDescriptor = UAEndpointDescriptor('opc.tcp://localhost:48030')
endpointDescriptor = UAEndpointDescriptorExtension.WithUserNameIdentity(endpointDescriptor,'john', 'master')

# A node that represents an instance of OPC UA FileType object.
fileNodeDescriptor = UANodeDescriptor('nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile')

# Instantiate the file transfer client object.
fileTransferClient = EasyUAFileTransferClient()

# Prevent prompt to trust the server certificate (INSECURE, used just for smooth example flow).
EasyUAClient.SharedParameters.EngineParameters.CertificateAcceptancePolicy.TrustEndpointUrlString(
    endpointDescriptor.UrlString)

# Write all contents into a specified file node.
bytes = Encoding.UTF8.GetBytes('TEXT FROM FILE TRANSFER CLIENT EXAMPLE. Demonstrates writing the whole contents of a '
                               'file at once.')
try:
    print('Writing the whole file...')
    IEasyUAFileTransferExtension2.WriteAllBytes(fileTransferClient,
                                                endpointDescriptor,
                                                UANamedNodeDescriptor(fileNodeDescriptor),
                                                bytes)

    # Due to an issue in the server, the file might not be readable now, without server restart.
    print('Reading the data back...')
    data = IEasyUAFileTransferExtension2.ReadAllBytes(fileTransferClient,
                                                       endpointDescriptor,
                                                       UANamedNodeDescriptor(fileNodeDescriptor))
    print(Encoding.UTF8.GetString(data))

# Beware that ReadAllFileBytes throws IOException and not UAException.
except IOException as ioException:
    print('*** Failure: ' + ioException.GetBaseException().Message)
    exit()

print()
print('Finished.')
' Shows how to write the full contents of an OPC UA file at once, using the file transfer client.
' Note: Consider using a higher-level abstraction, OPC UA file provider, instead.

Imports System.IO
Imports System.Text
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.FileTransfer
Imports OpcLabs.EasyOpc.UA.IO.Extensions

Namespace FileTransfer._EasyUAFileTransferClient

    Friend Class WriteAllBytes

        Public Shared Sub Main1()

            ' Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe)
            Dim endpointDescriptor As UAEndpointDescriptor = "opc.tcp://localhost:48030"

            ' A node that represents an instance of OPC UA FileType object.
            Dim fileNodeDescriptor As UANodeDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile"

            ' Instantiate the file transfer client object
            Dim fileTransferClient = New EasyUAFileTransferClient

            ' Write all contents into a specified file node.
            Dim bytes As Byte() = Encoding.UTF8.GetBytes("TEXT FROM FILE TRANSFER CLIENT EXAMPLE. Demonstrates writing the whole contents of a file at once.")
            Try
                Console.WriteLine("Writing the whole file...")
                fileTransferClient.WriteAllBytes(endpointDescriptor, fileNodeDescriptor, bytes)

                ' Due to an issue in the server, the file might Not be readable now, without server restart.
                'Console.WriteLine("Reading the data back...")
                'Dim data As Byte() = fileTransferClient.ReadAllFileBytes(endpointDescriptor, fileNodeDescriptor)
                'Console.WriteLine(Encoding.UTF8.GetString(data))

                ' Beware that WriteAllFileBytes throws IOException And Not UAException.
            Catch ioException As IOException
                Console.WriteLine("*** Failure: {0}", ioException.GetBaseException.Message)
                Exit Sub
            End Try

            Console.WriteLine()
            Console.WriteLine("Finished...")
        End Sub
    End Class
End Namespace
Requirements

Target Platforms: .NET Framework: Windows 10 (selected versions), Windows 11 (selected versions), Windows Server 2016, Windows Server 2022; .NET: Linux, macOS, Microsoft Windows

See Also